<?php

/*
 *  Author: dimitris
 *  Improved by: basanas
 *  Returns all the categories visible to a specific user
 */

function BL_getVisibleCategories( $userid )
{
    $publicCategories = BL_getPublicCategories();
    if ( is_null( $userid ) || ( $userid == -1 ) )
        return $publicCategories;

    $ownedCategories = BL_get_categories_with_owner( $userid );
    if ( $ownedCategories == BL_GET_CATEGORIES_WITH_OWNER )
        return BL_GET_CATEGORIES_WITH_OWNER;

    $moderatedCategories = BL_get_categories_with_moderator( $userid );
    if ( $moderatedCategories == BL_GET_CATEGORIES_WITH_MODERATOR )
        return BL_GET_CATEGORIES_WITH_MODERATOR;

    $invitedCategories = BL_get_categories_with_member( $userid );
    if ( $invitedCategories == BL_GET_CATEGORIES_WITH_MEMBER )
        return BL_GET_USR_PRIVATE_CATEGORIES;


    $categories = $publicCategories;
    if($ownedCategories!=NULL)
        $categories = array_merge($categories,$ownedCategories);
    
    if($moderatedCategories!=NULL)
        $categories = array_merge($categories,$moderatedCategories);
    
    if($invitedCategories!=NULL)
        $categories = array_merge($categories,$invitedCategories);
        
    $unique = array();
    $k=0;
    for($i=0; $i<count($categories);$i++){
        for($j=$i+1; $j<count($categories);$j++){
            if($categories[$i][0] == $categories[$j][0] ){
                break;
            }
        }   
        if($j == count($categories)){
            $unique[$k] = $categories[$i];
            $k++;
        }
    }
    return $categories;
}



/*  Author: basanas
    Returns all the categories where a user is either owner or mod
 */
function BL_getOwnerModCategories( $userid )
{
    if ( is_null( $userid ) || ( $userid == -1 ) )
        return Array();

    $ownedCategories = BL_get_categories_with_owner( $userid );
    if ( $ownedCategories == BL_GET_CATEGORIES_WITH_OWNER )
        return BL_GET_CATEGORIES_WITH_OWNER;

    $moderatedCategories = BL_get_categories_with_moderator( $userid );
    if ( $moderatedCategories == BL_GET_CATEGORIES_WITH_MODERATOR )
        return BL_GET_CATEGORIES_WITH_MODERATOR;

    $categories = array();

    if($ownedCategories !=NULL)
        $categories = array_merge( $categories, $ownedCategories);
    
    if($moderatedCategories != NULL)
  		$categories = array_merge( $categories, $moderatedCategories); 
	
    return $categories;
}

/*  Author: basanas
    Returns all the categories where a user is either owner or mod
 */
function BL_getUserCategories( $userid )
{
    if ( is_null( $userid ) || ( $userid == -1 ) )
        return Array();

    $ownedCategories = BL_get_categories_with_owner( $userid );
    if ( $ownedCategories == BL_GET_CATEGORIES_WITH_OWNER )
        return BL_GET_CATEGORIES_WITH_OWNER;

    $moderatedCategories = BL_get_categories_with_moderator( $userid );
    if ( $moderatedCategories == BL_GET_CATEGORIES_WITH_MODERATOR )
        return BL_GET_CATEGORIES_WITH_MODERATOR;

    $invitedCategories=BL_get_categories_with_member( $userid );
    if ( $invitedCategories == BL_GET_CATEGORIES_WITH_MEMBER )
        return BL_GET_USR_PRIVATE_CATEGORIES;
 	
    $categories = array();
    if($ownedCategories!=NULL)
        $categories = array_merge($categories,$ownedCategories);
    
    if($moderatedCategories!=NULL)
        $categories = array_merge($categories,$moderatedCategories);
    
    if($invitedCategories!=NULL)
        $categories = array_merge($categories,$invitedCategories);
        
    $unique = array();
    $k=0;
    for($i=0; $i<count($categories);$i++){
        for($j=$i+1; $j<count($categories);$j++){
            if($categories[$i][0] == $categories[$j][0] ){
                break;
            }
        }   
        if($j == count($categories)){
            $unique[$k] = $categories[$i];
            $k++;
        }
    }
    return $unique;
}

?>
